/*
 * @Author: wangtao
 * @Date: 2020-06-30 20:02:03
 * @LastEditors: 汪滔
 * @LastEditTime: 2021-06-17 10:34:40
 * @Description: 工具
 */
import { Dimensions, Platform } from 'react-native';
import baseConfig from '@/config/baseConfig';

const { apiObj } = baseConfig;

// 防抖
let timeout = null;
export function debounce(cb, wait = 500) {
  if (timeout !== null) clearTimeout(timeout);
  timeout = setTimeout(() => {
    timeout = null;
    cb && cb();
  }, wait);
}

/**
 * 是否是iPhonex
 */
export function isIphoneX() {
  const dimen = Dimensions.get('window');
  return (
    Platform.OS === 'ios' &&
    !Platform.isPad &&
    !Platform.isTVOS &&
    (dimen.height >= 812 || dimen.width >= 812)
  );
}

export function ifIphoneX(iphoneXStyle, regularStyle) {
  if (isIphoneX()) {
    return iphoneXStyle;
  }
  return regularStyle;
}

/**
 * @author kanglang
 * @description 支付方式
 */
export function orderPayWay(type) {
  switch (type) {
  case 0:
    return '未支付';
  case 1:
    return '微信公众号';
  case 2:
    return '微信'; // 安卓
  case 3:
    return '支付宝'; // H5
  case 4:
    return '支付宝'; // 安卓
  case 5:
    return '银行卡';
  case 6:
    return '线下支付';
  case 7:
    return '金豆支付';
  case 8:
    return '支付宝'; // ios
  case 9:
    return '微信';
  case 11:
    return '运费预付';
  case 11:
    return '运费预付'; // ios
  case 13:
    return '维修卡支付';
  }
}

/**
 * @author kanglang
 * @description 计算小数点后两位
 */
export function calculatJD(value, rate) {
  const calculatValue =
    (value && (Number(value) * Number(rate || 100)) / 100) || 0;
  return calculatValue.toFixed(2);
}

export function checkMobile(mobile) {
  const regEx = /^1(3|4|5|6|7|8|9)\d{9}$/;
  return regEx.test(mobile);
}

// 富文本文章处理
export const customStyle = ` 
<head>
  <meta name="viewport" content="width=device-width,initial-scale=0.5,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
</head>
<style>
  * {max-width: 100%;}
  html{margin-top:10px}
  img { padding: 0; margin: 0; border: 0}
  p,li,ul,span{margin-left: 10px; margin-right:10px;}
</style>
<script>
document.documentElement.addEventListener('touchstart', function (event) {
  if (event.touches.length > 1) {
    event.preventDefault();
  }
}, false);

var lastTouchEnd = 0;
document.documentElement.addEventListener('touchend', function (event) {
  var now = Date.now();
  if (now - lastTouchEnd <= 300) {
    event.preventDefault();
  }
  lastTouchEnd = now;
}, false);
document.addEventListener('gesturestart', function(event) {
  event.preventDefault();
});
document.addEventListener('gestureend', function(event) {
  event.preventDefault();
});
document.addEventListener('gesturechange', function(event) {
  event.preventDefault();
});
document.addEventListener('touchmove', function (event) {
  if (event.scale !== 1) { event.preventDefault(); }
}, false);
</script

`;

// 清掉输入的非数字
export function clearNoNum(val) {
  // 先把非数字的都替换掉，除了数字和.
  val = val.replace(/[^\d.]/g, '');
  // 必须保证第一个为数字而不是.
  val = val.replace(/^\./g, '');
  // 保证只有出现一个.而没有多个.
  val = val.replace(/\.{2,}/g, '.');
  // 保证.只出现一次，而不能出现两次以上
  val = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
  return val;
}

// 输入不能超过两位小数
export function clearDotNumTo2(val) {
  if (/^00/.test(val)) {
    return '0';
  }
  if (
    val.indexOf('.') !== -1 &&
    val.substring(val.indexOf('.'), val.length).length > 2
  ) {
    val = val.slice(0, val.indexOf('.') + 3);
  }
  return val;
}

// 根据经纬度计算距离 返回值 千米（km）
export function getDistance(lat1, lng1, lat2, lng2) {
  let radLat1 = (lat1 * Math.PI) / 180.0;
  let radLat2 = (lat2 * Math.PI) / 180.0;
  let a = radLat1 - radLat2;

  let b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
  let s =
    2 *
    Math.asin(
      Math.sqrt(
        Math.pow(Math.sin(a / 2), 2) +
        Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2),
      ),
    );
  s *= 6378.137; // EARTH_RADIUS;
  s = s.toFixed(1);
  return s;
}

export function checkCurrentEnv() {
  const currentUrl = apiObj.host;
  if (currentUrl === 'http://gateway-dev.cqxmgj.lo:9100/') {
    return '(Dev)';
  } if (currentUrl === 'https://api-test.guojutech.net/') {
    return '(Test)';
  } if (currentUrl === 'https://api-uat.guojutech.net/') {
    return '(UAT)';
  } if (currentUrl === 'https://api-pre.guojutech.net/') {
    return '(PRE)';
  }
  return '';
}
